package com.ztjava.pay.controller;

import java.util.Date;

import com.jfinal.aop.Duang;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;
import com.xiaoleilu.hutool.date.DateUtil;
import com.ztjava.core.Constant;
import com.ztjava.core.annotation.ControllerClass;
import com.ztjava.core.base.BaseController;
import com.ztjava.core.client.PortalClient;
import com.ztjava.ext.auth.annotation.UserController;
import com.ztjava.ext.auth.annotation.UserMethod;
import com.ztjava.ext.auth.bean.AuthSession;
import com.ztjava.pay.model.PayOrder;
import com.ztjava.pay.service.PayService;

@ControllerClass(path="/pay",client=PortalClient.class)
@UserController(portal=Constant.P_PORTAL)
public class PayPortalController extends BaseController{
	/*************
	 *PayAdminController.java
	 *@ztjava.com
	 *2018-09-20 09:09:12
	**************/
	private static PayService payService = Duang.duang(PayService.class);
	/*************
	 *修改Order
	**************/
	@UserMethod("PayOrder")
	public void addEditOrder(){
		Integer id = getParaToInt("id",0);
		PayOrder payOrder = new PayOrder();
		if(id>0){
			payOrder = PayOrder.dao.findById(id);
		}
		setAttr("payOrder",payOrder);
		render("addEditOrder.html");
	}

	@UserMethod("PayOrder")
	public void changeRepair(){
		String type= getPara("type","T");
		Cache cache = Redis.use();
		if(type.equals("T")){
			cache.del(Constant.R_PRE+"PAY:REPAIR");
		}else {
			cache.set(Constant.R_PRE+"PAY:REPAIR", "T");
		}
		renderSuccess();
	}

	/*************
	 *保存Order
	**************/
	@UserMethod("PayOrder")
	public void saveOrder(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		PayOrder payOrder = getModel(PayOrder.class);
		saveModel(payOrder,authSession.getId());
		renderSuccess();
	}

	/*************
	 *Order列表
	**************/
	@UserMethod("PayOrder")
	public void orderList(){
		
		if(!getPara("is_ajax","F").equals("T")){
			setAttr("start_time", DateUtil.offsetMonth(new Date(), -1).toString("yyyy-MM-dd 00:00:00"));
			setAttr("end_time", DateUtil.date().toString("yyyy-MM-dd 23:59:59"));
			keepPara();
			render("orderList.html");
			return;
		}

		
		renderPageJson(payService.getOrderPage(client, getParaMap()));
	}

	/*************
	 *批量删除Order
	**************/
	@UserMethod("PayOrder")
	public void batchDeleteOrder(){
		String[] btSelectItem = getParaValues("btSelectItem");
		payService.batchDelete(PayOrder.tablename, btSelectItem);
		renderSuccess();
	}

}
